import { defineStore } from "pinia"
import { AuthState } from "@/stores/interface";
import { useRequest } from 'alova'
import { getAuthMenuListApi } from "@/api/login"
import { getFlatMenuList, getShowMenuList, getAllBreadcrumbList } from "@/utils";

const {
	// ...
	// 手动发送器请求的函数，调用后发送请求
	send: addTodo
  } = useRequest(getAuthMenuListApi, {
	// 当immediate为false时，默认不发出
	immediate: false
  });
// AuthStore
export const AuthStore = defineStore({
	id: "AuthState",
	state: (): AuthState => ({
		// 当前页面的 router name，用来做按钮权限筛选
		routeName: "",
		// 按钮权限列表
		authButtonList: {},
		// 菜单权限列表
		authMenuList: []
	}),
    getters: {
		// 菜单权限列表 ==> 这里的菜单没有经过任何处理
		authMenuListGet: state => state.authMenuList,
		// 菜单权限列表 ==> 左侧菜单栏渲染，需要剔除 isHide == true
		showMenuListGet: state => getShowMenuList(state.authMenuList),
        // 菜单权限列表 ==> 扁平化之后的一维数组菜单，主要用来添加动态路由
		flatMenuListGet: state => getFlatMenuList(state.authMenuList),
		// 递归处理后的所有面包屑导航列表
		breadcrumbListGet: state => getAllBreadcrumbList(state.authMenuList)
    },
	actions: {
		getAuthMenuList() {
			// const { data } = useRequest(getAuthMenuListApi, {
			// 	// 请求响应前，data的初始值
			// 	initialData: []
			// })
			addTodo()
			.then((result: any) => {
				console.log('新增todo项成功，响应数据为:', result);
				this.authMenuList = result
			})
			// console.log('data', data)
			// this.authMenuList = data
		},
	}
})